<?php

namespace App\Kuafu\Shop\Controller\B;

use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\Shop\Exceptions\ShopException;
use App\Kuafu\Shop\Service\ToolsService;
use App\Kuafu\User\Exceptions\UserException;
use App\Kuafu\User\Service\AuthService;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;


class ToolsController extends BaseController
{


    /**
     * 物料素材模版列表
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function list(Request $request): JsonResponse
    {
        $pageSize = (int)$request->get('pageSize', 20);
        $pageNum  = (int)$request->get('pageNum', 1);
        $type     = (int)$request->get('type');
        $name     = (string)$request->get('name', '');//名称
        return $this->success(ToolsService::totalList($type, $name, $pageSize, $pageNum));
    }


    /**
     * 物料素材模版单个详细-web
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function info(Request $request, int $id): JsonResponse
    {
        return $this->success(ToolsService::info($id));
    }

    /**
     * 物料素材模版配置
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function typeConfig(Request $request): JsonResponse
    {
        return $this->success(ToolsService::typeConfig());
    }

    /**
     * 新增物料
     * @param Request $request
     * @return JsonResponse
     * @throws UserException
     * @throws Exception
     */
    public function save(Request $request): JsonResponse
    {
        $name             = (string)$request->get('name', '');//名称
        $url              = (string)$request->get('url', '');//预览图
        $reverseUrl       = (string)$request->get('reverseUrl', '');//背面预览图
        $thumbnail        = (string)$request->get('thumbnail', '');//缩略图
        $reverseThumbnail = (string)$request->get('reverseThumbnail', '');//背面缩略图
        $value            = (string)$request->get('value', '');//模版配置信息
        $appValue         = (string)$request->get('appValue', '');//app模版配置信息
        $size             = (string)$request->get('size', '');//模版图片尺寸
        $sort             = (int)$request->get('sort', '');//排序 越小越靠前
        $type             = (int)$request->get('type', '');//类型
        $id               = (int)$request->get('id', 0);

        if (!$value) {
            $value = null;
        }
        if (!$appValue) {
            $appValue = null;
        }
        $userId = AuthService::getUserId();
        ToolsService::save($id, $name, $thumbnail, $type, $url, $reverseUrl, $reverseThumbnail,
            $value, $appValue, $size, $sort, $userId);
        return $this->success();
    }


    /**
     * 物料素材模版-删除
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws ShopException|UserException
     */
    public function delete(Request $request, int $id): JsonResponse
    {
        $userId = AuthService::getUserId();
        ToolsService::delete($id, $userId);
        return $this->success();
    }
}
